﻿using System;
using System.Linq;
using System.Windows.Forms;
using Kay.Cqp.App.Common;
using Kay.Cqp.App.MyForms;
using Newbe.CQP.Framework;
using Newbe.CQP.Framework.Extensions;

namespace Kay.Cqp.App
{
    /// <summary>
    /// 主插件
    /// </summary>
    public class MainPlugin : PluginBase
    {
        /// <summary>
        /// 初始化主插件
        /// </summary>
        /// <param name="coolQApi"></param>
        public MainPlugin(ICoolQApi coolQApi) : base(coolQApi)
        {

        }

        /// <summary>
        /// 当前程序 Id
        /// </summary>
        public override string AppId => "kay.cqp.app";

        /// <summary>
        /// 点击第一个按钮事件
        /// </summary>
        /// <returns></returns>
        public override int ProcessMenuClickA()
        {
            using (var frm = new MySettings())
            {
                frm.Qq = CoolQApi.GetLoginQQ();
                //frm.Quns = CoolQApi.GetGroupList();
                frm.ShowDialog();
            }
            return base.ProcessMenuClickA();
        }

        /// <summary>
        /// 请求-好友添加
        /// </summary>
        /// <param name="subType"></param>
        /// <param name="sendTime"></param>
        /// <param name="fromQq"></param>
        /// <param name="msg">附言</param>
        /// <param name="responseFlag">反馈标识(处理请求用)</param>
        /// <returns></returns>
        public override int ProcessAddFriendRequest(int subType, int sendTime, long fromQq, string msg, string responseFlag)
        {
            if (MySettingsConfig.Qq == default(long))
            {
                MySettingsConfig.Qq = CoolQApi.GetLoginQQ();
            }
            if (MySettingsConfig.ZidongTianjiaHaoyou())
            {
                CoolQApi.SetFriendAddRequest(responseFlag, 1, "");
            }

            return 0;
        }

        /// <summary>
        /// 群事件-群成员增加
        /// </summary>
        /// <param name="subType">子类型，1/管理员已同意 2/管理员邀请</param>
        /// <param name="sendTime"></param>
        /// <param name="fromGroup"></param>
        /// <param name="fromQq">操作者QQ(即管理员QQ)</param>
        /// <param name="target">被操作QQ(即加群的QQ)</param>
        /// <returns></returns>
        public override int ProcessGroupMemberIncrease(int subType, int sendTime, long fromGroup, long fromQq, long target)
        {
            if (MySettingsConfig.Qq == default(long))
            {
                MySettingsConfig.Qq = CoolQApi.GetLoginQQ();
            }
            string msg;
            if (MySettingsConfig.ZidongFasongHuanyingyu(fromGroup.ToString(), out msg))
            {
                CoolQApi.SendGroupMsg(fromGroup, $"{CoolQCode.At(fromQq)} {msg}");
                return 1;
            }
            return 0;
        }

        /// <summary>
        /// 群消息
        /// </summary>
        /// <param name="subType"></param>
        /// <param name="sendTime"></param>
        /// <param name="fromGroup"></param>
        /// <param name="fromQq"></param>
        /// <param name="fromAnonymous"></param>
        /// <param name="msg"></param>
        /// <param name="font"></param>
        /// <returns></returns>
        public override int ProcessGroupMessage(int subType, int sendTime, long fromGroup, long fromQq, string fromAnonymous, string msg,
            int font)
        {
            if (MySettingsConfig.Qq == default(long))
            {
                MySettingsConfig.Qq = CoolQApi.GetLoginQQ();
            }
            bool notAgree;
            int day;
            if (MySettingsConfig.ZidongTichu(fromGroup.ToString(), msg, out notAgree))
            {
                CoolQApi.SetGroupKick(fromGroup, fromQq, notAgree);
                return 1;
            }
            if (MySettingsConfig.ZidongJinyan(fromGroup.ToString(), msg, out day))
            {
                if (fromQq == 80000000)
                    CoolQApi.SetGroupAnonymousBan(fromGroup, fromAnonymous, 60 * 60 * 24 - 10);
                else
                    CoolQApi.SetGroupBan(fromGroup, fromQq, day * 24 * 60 * 60);
                return 1;
            }
            return 0;
        }

        /// <summary>
        /// 请求-群添加
        /// </summary>
        /// <param name="subType">子类型，1/他人申请入群 2/自己(即登录号)受邀入群</param>
        /// <param name="sendTime"></param>
        /// <param name="fromGroup"></param>
        /// <param name="fromQq"></param>
        /// <param name="msg">附言</param>
        /// <param name="responseMark">反馈标识(处理请求用)</param>
        /// <returns></returns>
        public override int ProcessJoinGroupRequest(int subType, int sendTime, long fromGroup, long fromQq, string msg, string responseMark)
        {
            if (MySettingsConfig.Qq == default(long))
            {
                MySettingsConfig.Qq = CoolQApi.GetLoginQQ();
            }
            var r = MySettingsConfig.ZidongTongyiRuqun(subType, fromGroup.ToString());
            if (!r) return 0;
            switch (subType)
            {
                case 1:
                    CoolQApi.SetGroupAddRequest(responseMark, CoolQAddGroupRequestType.Normal,
                        CoolQRequestResult.Allow);
                    break;
                case 2:
                    CoolQApi.SetGroupAddRequest(responseMark, CoolQAddGroupRequestType.Invtiation,
                        CoolQRequestResult.Allow);
                    break;
            }
            return 1;
        }
    }
}
